From a23c49ba6311497e1b5bdbf8ab4b363ce25aad8b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 22 Oct 2014 22:05:30 -0700 Subject: [PATCH] Rewrite registry tests for easier mocking and such --- tests/fixtures/bar-0.0.1.tar.gz | Bin 295 -> 0 bytes tests/fixtures/foo-0.0.1.tar.gz | Bin 274 -> 0 bytes tests/fixtures/hello.rs | 3 - tests/fixtures/notyet-0.0.1.tar.gz | Bin 284 -> 0 bytes tests/support/mod.rs | 1 + tests/support/registry.rs | 124 +++++++++++++++++++++++++++++ tests/test_cargo_registry.rs | 112 ++++++-------------------- 7 files changed, 148 insertions(+), 92 deletions(-) delete mode 100644 tests/fixtures/bar-0.0.1.tar.gz delete mode 100644 tests/fixtures/foo-0.0.1.tar.gz delete mode 100644 tests/fixtures/hello.rs delete mode 100644 tests/fixtures/notyet-0.0.1.tar.gz create mode 100644 tests/support/registry.rs diff --git a/tests/fixtures/bar-0.0.1.tar.gz b/tests/fixtures/bar-0.0.1.tar.gz deleted file mode 100644 index 55b2f5a6c6f79f2e256579bd7d6934d5c9d94e2e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 295 zcmV+?0oeW@iwFn+0000217cxvEif)HE-@~2VR9~KdI0T~!ES^g6h&S06~u1ZMG*mY zHQn?xjA_t$wL}Ldz_g2||6X*Gv0cp0PMXMB_#)hdK;9v=(mql`NuD{|Wut0|b;-(< zl+HD;3i`8hwq$Z9DCdPJd6`t~JWrDwaB^N+jFXLWS9@^2L9ZGs|MFq%;O-yrC;CEL zJ&|qEUi>G$@~YRy!rE(G^Ph<&^UHr8K1uZd7Gz=fJ$RHohib@Zx*?jsAr}A74dBgT zTzidc{^zW^WM=;hDnbqZi+LjY|M%2K*$uLX25ET!yu)eeh!nkpb;{`Jf9{SH$&-IG t)`gL}L8#ut5%eB(r@)2RgE3R|X@{pYP2&|3i9{m*op0>t)O-LC0045~k+=W= diff --git a/tests/fixtures/foo-0.0.1.tar.gz b/tests/fixtures/foo-0.0.1.tar.gz deleted file mode 100644 index bf1fc952707b6f0c12a54dbedb6b9601bb8bbc7f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 274 zcmV+t0qy=DiwFn+0000217>e;Eif)HE-@~2VR9~KdI0T}y>bE}6h^6e3Z&ARWQ7GT zRjM?32gib#)$D`?2iBy>`0lb=_&0W|Gb*Rx4tJ>d?t!`YudHANAIC5oH>P$)h^SwH zv2EpAvBRq5{tPb#XeE^@HPRA9*JJ1gtYT{dA?Qh&JJN+d9Fc!a63bA#*1ExvepJ@_ z7xL$W{Tk2_UgKxIqleD_$bVaZ_OCwz*ol7X|DoT!luQT^9FqSf&;H*$|92A>zG!`8 zd(Z#E`wwuEzhY^TNDF>8A36W)Fj<<}k6Bbx>`YUUH$u}yQ2bkkSbNv)x6?tfS+(Cj Y#MGHg^G}e;WHNWp2C$DPmH-X_0Aj3zApigX diff --git a/tests/fixtures/hello.rs b/tests/fixtures/hello.rs deleted file mode 100644 index e7a694f5a..000000000 --- a/tests/fixtures/hello.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("WINNING"); -} diff --git a/tests/fixtures/notyet-0.0.1.tar.gz b/tests/fixtures/notyet-0.0.1.tar.gz deleted file mode 100644 index 5b8ae93676d68448bed643fa5b13b68e890a7fb5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 284 zcmV+%0ptE3iwFn+0000218#40d1Z7hFfK4IF)nmraxQ0j0PU5}ZUP|?hdJ{U$VroS z9a#RHdg`U`U_4Mu?WQakz&1_Ockil)+D*KzCc1o=Nd`{*<{Ny-UpC)E1r?j8xSF@7 z@l{M{Kor2fGA-e_O7VAx@mgsqq~sbg;GB0oKo?+}vWbMCk@D)vF7fG%d}GqFiH*m diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 8036060b1..cb8f16443 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -15,6 +15,7 @@ use support::paths::PathExt; pub mod paths; pub mod git; +pub mod registry; /* * diff --git a/tests/support/registry.rs b/tests/support/registry.rs new file mode 100644 index 000000000..c25953e60 --- /dev/null +++ b/tests/support/registry.rs @@ -0,0 +1,124 @@ +use std::io::{mod, fs, File}; + +use flate2::Default; +use flate2::writer::GzEncoder; +use git2; +use serialize::hex::ToHex; +use tar::Archive; +use url::Url; + +use support::{ResultTest, project}; +use support::paths; +use support::git::repo; +use cargo::util::Sha256; + +pub fn registry_path() -> Path { paths::root().join("registry") } +pub fn registry() -> Url { Url::from_file_path(®istry_path()).unwrap() } +pub fn dl_path() -> Path { paths::root().join("dl") } +pub fn dl_url() -> Url { Url::from_file_path(&dl_path()).unwrap() } + +pub fn init() { + let config = paths::root().join(".cargo/config"); + fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert(); + File::create(&config).write_str(format!(r#" + [registry] + index = "{reg}" + token = "api-token" + "#, reg = registry()).as_slice()).assert(); + + // Init a new registry + repo(®istry_path()) + .file("config.json", format!(r#" + {{"dl":"{}","api":""}} + "#, dl_url()).as_slice()) + .build(); +} + +pub fn mock_archive(name: &str, version: &str, deps: &[(&str, &str)]) { + let mut manifest = format!(r#" + [package] + name = "{}" + version = "{}" + authors = [] + "#, name, version); + for &(dep, req) in deps.iter() { + manifest.push_str(format!(r#" + [dependencies.{}] + version = "{}" + "#, dep, req).as_slice()); + } + let p = project(name) + .file("Cargo.toml", manifest.as_slice()) + .file("src/lib.rs", ""); + p.build(); + + let dst = mock_archive_dst(name, version); + fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).assert(); + let f = File::create(&dst).unwrap(); + let a = Archive::new(GzEncoder::new(f, Default)); + a.append(format!("{}-{}/Cargo.toml", name, version).as_slice(), + &mut File::open(&p.root().join("Cargo.toml")).unwrap()).unwrap(); + a.append(format!("{}-{}/src/lib.rs", name, version).as_slice(), + &mut File::open(&p.root().join("src/lib.rs")).unwrap()).unwrap(); + a.finish().unwrap(); +} + +pub fn mock_archive_dst(name: &str, version: &str) -> Path { + dl_path().join(name).join(version).join("download") +} + +pub fn mock_pkg(name: &str, version: &str, deps: &[(&str, &str)]) { + mock_archive(name, version, deps); + let c = File::open(&mock_archive_dst(name, version)).read_to_end().unwrap(); + let line = pkg(name, version, deps, cksum(c.as_slice()).as_slice()); + + let file = match name.len() { + 1 => format!("1/{}", name), + 2 => format!("2/{}", name), + 3 => format!("3/{}/{}", name.slice_to(1), name), + _ => format!("{}/{}/{}", name.slice(0, 2), name.slice(2, 4), name), + }; + publish(file.as_slice(), line.as_slice()); +} + +pub fn publish(file: &str, line: &str) { + let repo = git2::Repository::open(®istry_path()).unwrap(); + let mut index = repo.index().unwrap(); + { + let dst = registry_path().join(file); + let prev = File::open(&dst).read_to_string().unwrap_or(String::new()); + fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).unwrap(); + File::create(&dst).write_str((prev + line + "\n").as_slice()).unwrap(); + } + index.add_path(&Path::new(file)).unwrap(); + index.write().unwrap(); + let id = index.write_tree().unwrap(); + let tree = repo.find_tree(id).unwrap(); + let sig = repo.signature().unwrap(); + let parent = repo.refname_to_id("refs/heads/master").unwrap(); + let parent = repo.find_commit(parent).unwrap(); + repo.commit(Some("HEAD"), &sig, &sig, + "Another commit", &tree, + [&parent]).unwrap(); +} + +pub fn pkg(name: &str, vers: &str, deps: &[(&str, &str)], cksum: &str) -> String { + let deps = deps.iter().map(|&(a, b)| dep(a, b)).collect::>(); + format!(r#"{{"name":"{}","vers":"{}","deps":{},"cksum":"{}","features":{{}}}}"#, + name, vers, deps, cksum) +} + +pub fn dep(name: &str, req: &str) -> String { + format!("{{\"name\":\"{}\",\ + \"req\":\"{}\",\ + \"features\":[],\ + \"default_features\":false,\ + \"target\":null,\ + \"optional\":false}}", name, req) +} + +pub fn cksum(s: &[u8]) -> String { + let mut sha = Sha256::new(); + sha.update(s); + sha.finish().to_hex() +} diff --git a/tests/test_cargo_registry.rs b/tests/test_cargo_registry.rs index b5fe5bc8b..c77b993a2 100644 --- a/tests/test_cargo_registry.rs +++ b/tests/test_cargo_registry.rs @@ -1,87 +1,13 @@ -use std::io::{mod, fs, File}; -use url::Url; -use git2; -use serialize::hex::ToHex; +use std::io::File; -use support::{ResultTest, project, execs, cargo_dir}; +use support::{project, execs, cargo_dir}; use support::{UPDATING, DOWNLOADING, COMPILING, PACKAGING, VERIFYING}; -use support::paths; -use support::git::repo; -use cargo::util::Sha256; +use support::registry as r; use hamcrest::assert_that; -fn registry_path() -> Path { paths::root().join("registry") } -fn registry() -> Url { Url::from_file_path(®istry_path()).unwrap() } -fn dl_path() -> Path { paths::root().join("dl") } -fn dl_url() -> Url { Url::from_file_path(&dl_path()).unwrap() } - -fn cksum(s: &[u8]) -> String { - let mut sha = Sha256::new(); - sha.update(s); - sha.finish().to_hex() -} - fn setup() { - let config = paths::root().join(".cargo/config"); - fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert(); - File::create(&config).write_str(format!(r#" - [registry] - index = "{reg}" - token = "api-token" - "#, reg = registry()).as_slice()).assert(); - - // Prepare the "to download" artifacts - let foo = include_bin!("fixtures/foo-0.0.1.tar.gz"); - let bar = include_bin!("fixtures/bar-0.0.1.tar.gz"); - let notyet = include_bin!("fixtures/notyet-0.0.1.tar.gz"); - let foo_cksum = dl("foo", "0.0.1", foo); - let bar_cksum = dl("bar", "0.0.1", bar); - dl("bad-cksum", "0.0.1", foo); - let notyet = dl("notyet", "0.0.1", notyet); - - // Init a new registry - repo(®istry_path()) - .file("config.json", format!(r#" - {{"dl":"{}","api":""}} - "#, dl_url()).as_slice()) - .file("3/f/foo", pkg("foo", "0.0.1", [], &foo_cksum)) - .file("3/b/bar", pkg("bar", "0.0.1", [ - "{\"name\":\"foo\",\ - \"req\":\">=0.0.0\",\ - \"features\":[],\ - \"default_features\":false,\ - \"target\":null,\ - \"optional\":false}" - ], &bar_cksum)) - .file("ba/d-/bad-cksum", pkg("bad-cksum", "0.0.1", [], &bar_cksum)) - .nocommit_file("no/ty/notyet", pkg("notyet", "0.0.1", [], ¬yet)) - .build(); - - fn pkg(name: &str, vers: &str, deps: &[&str], cksum: &String) -> String { - format!(r#"{{"name":"{}","vers":"{}","deps":{},"cksum":"{}","features":{{}}}}"#, - name, vers, deps, cksum) - } - fn dl(name: &str, vers: &str, contents: &[u8]) -> String { - let dst = dl_path().join(name).join(vers).join("download"); - fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).assert(); - File::create(&dst).write(contents).unwrap(); - cksum(contents) - } -} - -fn publish_notyet() { - let repo = git2::Repository::open(®istry_path()).unwrap(); - let mut index = repo.index().unwrap(); - index.add_path(&Path::new("no/ty/notyet")).unwrap(); - let id = index.write_tree().unwrap(); - let tree = repo.find_tree(id).unwrap(); - let sig = repo.signature().unwrap(); - let parent = repo.refname_to_id("refs/heads/master").unwrap(); - let parent = repo.find_commit(parent).unwrap(); - repo.commit(Some("HEAD"), &sig, &sig, - "Another commit", &tree, - [&parent]).unwrap(); + r::init(); } test!(simple { @@ -93,33 +19,35 @@ test!(simple { authors = [] [dependencies] - foo = ">= 0.0.0" + bar = ">= 0.0.0" "#) .file("src/main.rs", "fn main() {}"); + r::mock_pkg("bar", "0.0.1", []); + assert_that(p.cargo_process("build"), execs().with_status(0).with_stdout(format!("\ {updating} registry `{reg}` -{downloading} foo v0.0.1 (the package registry) -{compiling} foo v0.0.1 (the package registry) +{downloading} bar v0.0.1 (the package registry) +{compiling} bar v0.0.1 (the package registry) {compiling} foo v0.0.1 ({dir}) ", updating = UPDATING, downloading = DOWNLOADING, compiling = COMPILING, dir = p.url(), - reg = registry()).as_slice())); + reg = r::registry()).as_slice())); // Don't download a second time assert_that(p.cargo_process("build"), execs().with_status(0).with_stdout(format!("\ {updating} registry `{reg}` -[..] foo v0.0.1 (the package registry) +[..] bar v0.0.1 (the package registry) [..] foo v0.0.1 ({dir}) ", updating = UPDATING, dir = p.url(), - reg = registry()).as_slice())); + reg = r::registry()).as_slice())); }) test!(deps { @@ -135,12 +63,15 @@ test!(deps { "#) .file("src/main.rs", "fn main() {}"); + r::mock_pkg("baz", "0.0.1", []); + r::mock_pkg("bar", "0.0.1", [("baz", "*")]); + assert_that(p.cargo_process("build"), execs().with_status(0).with_stdout(format!("\ {updating} registry `{reg}` {downloading} [..] v0.0.1 (the package registry) {downloading} [..] v0.0.1 (the package registry) -{compiling} foo v0.0.1 (the package registry) +{compiling} baz v0.0.1 (the package registry) {compiling} bar v0.0.1 (the package registry) {compiling} foo v0.0.1 ({dir}) ", @@ -148,7 +79,7 @@ test!(deps { downloading = DOWNLOADING, compiling = COMPILING, dir = p.url(), - reg = registry()).as_slice())); + reg = r::registry()).as_slice())); }) test!(nonexistent { @@ -185,6 +116,9 @@ test!(bad_cksum { "#) .file("src/main.rs", "fn main() {}"); + r::mock_pkg("bad-cksum", "0.0.1", []); + File::create(&r::mock_archive_dst("bad-cksum", "0.0.1")).unwrap(); + assert_that(p.cargo_process("build").arg("-v"), execs().with_status(101).with_stderr("\ Unable to get packages from source @@ -217,7 +151,7 @@ location searched: the package registry version required: >= 0.0.0 ")); - publish_notyet(); + r::mock_pkg("notyet", "0.0.1", []); assert_that(p.process(cargo_dir().join("cargo")).arg("build"), execs().with_status(0).with_stdout(format!("\ @@ -230,7 +164,7 @@ version required: >= 0.0.0 downloading = DOWNLOADING, compiling = COMPILING, dir = p.url(), - reg = registry()).as_slice())); + reg = r::registry()).as_slice())); }) test!(package_with_path_deps { @@ -265,7 +199,7 @@ location searched: the package registry version required: ^0.0.1 ")); - publish_notyet(); + r::mock_pkg("notyet", "0.0.1", []); assert_that(p.process(cargo_dir().join("cargo")).arg("package"), execs().with_status(0).with_stdout(format!("\ -- 2.30.2